<?php

namespace backend\models\financial;

use Yii;
use yii\data\Pagination;

/**
 * This is the model class for table "edu_cashbills".
 *
 * @property integer $bill_id
 * @property integer $uid
 * @property string $username
 * @property integer $billstatus
 * @property string $billmoney
 * @property integer $addtime
 * @property string $billserial
 */
class Cashbills extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'edu_cashbills';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['uid', 'billstatus', 'addtime'], 'integer'],
            [['billmoney'], 'number'],
            [['username'], 'string', 'max' => 50],
            [['billserial'], 'string', 'max' => 15]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'bill_id' => 'Bill ID',
            'uid' => 'Uid',
            'username' => 'Username',
            'billstatus' => 'Billstatus',
            'billmoney' => 'Billmoney',
            'addtime' => 'Addtime',
            'billserial' => 'Billserial',
        ];
    }
    
    /**
     * 现金账单查询   生成列表
     */
    public function getBilllist() {
        $post = Yii::$app->request->post();
        $session = Yii::$app->session;
        if(Yii::$app->request->get('del')==1){
            $session->remove('cash_username');
            $session->remove('cash_usertime');
        }

            $session->open();if((!empty($post['username'])||!empty($session->get('cash_username')))||(!empty($post['usertime']))||!empty($session->get('cash_usertime'))){     
            $session = Yii::$app->session;
            $session->open();
            if(!empty($post['usertime'])||!empty($session->get('cash_usertime'))){
				
                $post['usertime'] = $post['usertime']?$post['usertime']:$session->get('cash_usertime');
                switch ($post['usertime']) {
                    case 1 : $addtime = ['>','addtime',strtotime("-1 week")]; break;
                    case 2 : $addtime = ['>','addtime',strtotime("-2 week")]; break;
                    case 3 : $addtime = ['>','addtime',strtotime(date("Y-m-01",strtotime("-1 month")))]; break;
                    case 4 : $addtime = ['>','addtime',strtotime(date("Y-m-01",strtotime("-2 month")))]; break;
                    case 5 : $addtime = ['>','addtime',strtotime(date("Y-m-01",strtotime("-3 month")))]; break;
                    default: $addtime = ''; break;                    
                }
                
                $session->set('cash_usertime',$post['usertime']);
                $arr['usertime'] = $session->get('cash_usertime');
            }
            
            if((!empty($addtime)||!empty($session->get('cash_usertime')))&&(!empty($post['username'])||!empty($session->get('cash_username')))){
                $post['username'] = $post['username']?$post['username']:$session->get('cash_username');   
                
                $session->set('cash_username',$post['username']);
                
                $query = Cashbills::find()->where(['username'=>$session->get('cash_username')])
                ->andwhere($session->get('cash_usertime'));
                $arr['username'] = $session->get('cash_username');
                $arr['usertime'] = $session->get('cash_usertime');
            }elseif (!empty($addtime)||!empty($session->get('cash_usertime'))){
                
                $query = Cashbills::find()->where($addtime);
            }elseif(!empty($post['username'])||!empty($session->get('cash_username'))){
                $post['username'] = $post['username']?$post['username']:$session->get('cash_username');
                $session->set('cash_username',$post['username']);
                
                $query = Cashbills::find()->where(['username'=>$session->get('cash_username')]);
                $arr['username'] = $session->get('cash_username');
            }
            
        }else{
            $session = Yii::$app->session;
            $session->open();
            $session->remove('cash_username');
            $session->remove('cash_usertime');
            $query = Cashbills::find();
        }
        
		$countQuery = clone $query;
		$pages = new Pagination(['totalCount' => $countQuery->count(),'pageSize' => '6']);
		$models = $query->offset($pages->offset)
		->orderby('addtime desc')
        ->limit($pages->limit)
        ->all();
	    if(!isset($arr)){
		$arr['usertime']=0;
		$arr['username']='';
		}
		return ['pages' => $pages, 'lists' => $models,'where'=>$arr];
    }
    
    /**
     * 账单数据 查询导出
     */
    public function getInfo(){
        $post = Yii::$app->request->post();
        $session = Yii::$app->session;
        if(Yii::$app->request->get('del')==1){
            $session->remove('cash_username');
            $session->remove('cash_usertime');
        }
        $session->open();if((!empty($post['username'])||!empty($session->get('cash_username')))||(!empty($post['usertime']))||!empty($session->get('cash_usertime'))){
            $session = Yii::$app->session;
            $session->open();
            if(!empty($post['usertime'])||!empty($session->get('cash_usertime'))){
                $post['usertime'] = $post['usertime']?$post['usertime']:$session->get('cash_usertime');
                switch ($post['usertime']) {
                    case 1 : $addtime = ['>','addtime',strtotime("-1 week")]; break;
                    case 2 : $addtime = ['>','addtime',strtotime("-2 week")]; break;
                    case 3 : $addtime = ['>','addtime',strtotime(date("Y-m-01",strtotime("-1 month")))]; break;
                    case 4 : $addtime = ['>','addtime',strtotime(date("Y-m-01",strtotime("-2 month")))]; break;
                    case 5 : $addtime = ['>','addtime',strtotime(date("Y-m-01",strtotime("-3 month")))]; break;
                    default: $addtime = ''; break;
                }
        
                $session->set('cash_usertime',$post['usertime']);
                $arr['usertime'] = $session->get('cash_usertime');
            }
        
            if((!empty($addtime)||!empty($session->get('cash_usertime')))&&(!empty($post['username'])||!empty($session->get('cash_username')))){
                $post['username'] = $post['username']?$post['username']:$session->get('cash_username');
        
                $session->set('cash_username',$post['username']);
        
                $data = Cashbills::find()->where(['username'=>$session->get('cash_username')])
                ->andwhere($session->get('cash_usertime'))->asArray()->all();
                $arr['username'] = $session->get('cash_username');
                $arr['usertime'] = $session->get('cash_usertime');
            }elseif (!empty($addtime)||!empty($session->get('cash_usertime'))){
        
                $data = Cashbills::find()->where($addtime)->asArray()->all();
            }elseif(!empty($post['username'])||!empty($session->get('cash_username'))){
                $post['username'] = $post['username']?$post['username']:$session->get('cash_username');
                $session->set('cash_username',$post['username']);
        
                $data = Cashbills::find()->where(['username'=>$session->get('cash_username')])->asArray()->all();
                $arr['username'] = $session->get('cash_username');
            }        
        }else{
                $data = Cashbills::find()->asArray()->all();
        }
        return $data;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
